home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / T U R B O Language / Turbo C v2.0 / WORDCNT.C < prev    next >
Text File  |  1988-08-29  |  4KB  |  126 lines

  1. /*****
  2. *  WORDCNT.C - Sample program for the debugging tutorial
  3. *
  4. *  Copyright (c) 1988 Borland International. All rights reserved.
  5. *
  6. *  NOTE: This program is for use with the debugging tutorial
  7. *        in the debugging chapter of the User's Guide. It
  8. *        intentionally contains bugs.
  9. ******/
  10.  
  11. #include <stdio.h>
  12. #include <ctype.h>
  13.  
  14. #define MAXWORDLEN      16
  15. #define NUL             ((char)0)
  16. #define SPACE           ((char)0x20)
  17.  
  18. /*****
  19. * Find the next word in the line buffer.
  20. * IN:     wordptr points to the first character of a word or a preceding
  21. *         space.
  22. * RETURN: A pointer to the first character of the word. If there are no
  23. *         more words, a pointer to the terminating NUL.
  24. *****/
  25. char *nextword(char *wordptr)
  26. {
  27. /* Advance to the first non-space. */
  28.   while ( *wordptr==SPACE )
  29.     wordptr++;
  30.   return(wordptr);
  31. }
  32.  
  33.  
  34. /*****
  35. * Find the length of a word. A word is defined as a sequence of characters
  36. * terminated by a space or a NUL.
  37. * IN:    wordptr points to a word.
  38. * RETURN:The length of the word.
  39. *****/
  40. int wordlen(char *wordptr)
  41. {
  42.   char *wordlimit;
  43.   wordlimit = wordptr;
  44.   while ( *wordlimit & *wordlimit!=SPACE )
  45.     wordlimit++;
  46.   return( wordlimit-wordptr );
  47. }
  48.  
  49.  
  50. /*****
  51. * The main function.
  52. *****/
  53. void main(void)
  54. {
  55.   FILE    *infile;        /* Input file. */
  56.   char    linebfr[1024],  /* Input line buffer, very long for safety. */
  57.         *wordptr;       /* Pointer to next word in linebfr. */
  58.   int     i;            /* Scratch variable. */
  59.   static int    wordlencnt[MAXWORDLEN],
  60.                         /* Word lengths are counted in elements 1 to
  61.                          MAXWORDLEN. Element 0 isn't used. The array is
  62.                          static so that the elements need not be set to
  63.                          zero at run time. */
  64.             overlencnt;     /* Overlength words are counted here. */
  65.  
  66.   printf("WARNING: This is an example program for the practice\n");
  67.   printf("debugging session.  If you are not running this under the\n");
  68.   printf("Integrated Development Environment press control-break now.\n");
  69.   printf("See the debugging chapter of the User's Guide for details.\n\n");
  70.  
  71.   printf( "Enter the input file's name: " );
  72.   gets(linebfr);
  73.   if ( !strlen(linebfr) ) {
  74.     printf( "You must specify an input file name!\n" );
  75.     exit();
  76.   }
  77.  
  78.   infile = fopen( linebfr, "r" );
  79.   if ( !infile ) {
  80.     printf( "Can't open input file!\n" );
  81.     exit();
  82.   }
  83.  
  84. /* Each loop processes one line. NOTE: if a line is longer than the
  85.  input buffer the program may produce invalid results. The very large
  86.  buffer makes this unlikely. */
  87.   while (   fgets( linebfr, sizeof(linebfr), infile )   )   {
  88.     printf("%s\n",linebfr);
  89.     /* Check for buffer overflow & remove the trailing newline. */
  90.     i = strlen(linebfr);
  91.     if ( linebfr[i-1] != '\n' )
  92.       printf( "Overlength line beginning\n\t%70s\n", linebfr );
  93.     else
  94.       linebfr[i-1] = NUL;
  95.  
  96.     /* lineptr points to the 1st word in linebfr (past leading spaces). */
  97.     wordptr = nextword(linebfr);
  98.  
  99.     /* Each loop processes one word. The loop ends when [nextword]
  100.        returns NULL, indicating there are no more words. */
  101.     while (*wordptr) {
  102.     /* Find the length of this word, increment the proper element of the
  103.        length count array, & point to the space following the word. */
  104.       i = wordlen(wordptr);
  105.       if ( i > MAXWORDLEN )
  106.         overlencnt++;
  107.       else
  108.       ;
  109.       wordlencnt[i]++;
  110.     wordptr += i;
  111.  
  112.       /* Find the next word (if any). */
  113.       wordptr = nextword(wordptr);
  114.     }
  115.   }
  116.  
  117. /* Print the word length counts. Each loop prints one. */
  118.   printf(  "  Length Count\n" );
  119.   for ( i=1; i<MAXWORDLEN; i++ )
  120.     printf( "  %5d %5d\n", i, wordlencnt[i] );
  121.   printf( "Greater %5d\n", overlencnt );
  122.  
  123. /* Close the file & quit. */
  124.   fclose(infile);
  125. }
  126.